package com.example.servlet;

import com.example.dao.*;
import com.example.entity.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;

@WebServlet(name = "ManageServlet", value = "/Manage")
public class ManageServlet extends HttpServlet {

    private increaseDao increaseDao;
    private decreaseDao decreaseDao;
    private rentalDao rentalDao;
    private transactionDao transactionDao;

    @Override
    public void init() throws ServletException {
        try {
            increaseDao = new increaseDaoImpl();
            decreaseDao = new decreaseDaoImpl();
            rentalDao = new rentalDaoImpl();
            transactionDao = new transactionDaoImpl();
        } catch (Exception e) {
            throw new ServletException("DAO 初始化失败", e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        try {
            switch (action) {
                case "insertIncrease":
                    handleInsertIncrease(request);
                    break;
                case "insertDecrease":
                    handleInsertDecrease(request);
                    break;
                case "insertRental":
                    handleInsertRental(request);
                    break;
                case "insertTransaction":
                    handleInsertTransaction(request);
                    break;
                default:
                    response.sendRedirect("/jsp/Manage.jsp");
                    return;
            }
            response.sendRedirect("/jsp/Manage.jsp?status=success"); // 成功跳转
        } catch (Exception e) {
            e.printStackTrace();
            if ("饰品已存在".equals(e.getMessage())) {
                response.sendRedirect("/jsp/Manage.jsp?status=exists");
            } else {
                response.sendRedirect("/jsp/Manage.jsp?status=error");
            }
        }
    }

    // 插入涨幅榜数据
    private void handleInsertIncrease(HttpServletRequest request) throws Exception {
        String name = request.getParameter("name");
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        BigDecimal amount = new BigDecimal(request.getParameter("increaseAmount"));
        BigDecimal percentage = new BigDecimal(request.getParameter("increasePercentage"));
        Date date = parseDate(request.getParameter("date"));

        if (increaseDao.existsByName(name)) {
            throw new Exception("饰品已存在");
        }

        increase item = new increase();
        item.setName(name);
        item.setPrice(price);
        item.setIncreaseAmount(amount);
        item.setIncreasePercentage(percentage);
        item.setDate(date);
        increaseDao.insertIncreaseData(item);
    }

    // 插入跌幅榜数据
    private void handleInsertDecrease(HttpServletRequest request) throws Exception {
        String name = request.getParameter("name");
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        BigDecimal amount = new BigDecimal(request.getParameter("decreaseAmount"));
        BigDecimal percentage = new BigDecimal(request.getParameter("decreasePercentage"));
        Date date = parseDate(request.getParameter("date"));

        if (decreaseDao.existsByName(name)) {
            throw new Exception("饰品已存在");
        }

        decrease item = new decrease();
        item.setName(name);
        item.setPrice(price);
        item.setDecreaseAmount(amount);
        item.setDecreasePercentage(percentage);
        item.setDate(date);
        decreaseDao.insertDecreaseData(item);
    }

    // 插入成租榜数据
    private void handleInsertRental(HttpServletRequest request) throws Exception {
        String name = request.getParameter("name");
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        int volume = Integer.parseInt(request.getParameter("volume"));
        Date date = parseDate(request.getParameter("date"));

        if (rentalDao.existsByName(name)) {
            throw new Exception("饰品已存在");
        }

        rental item = new rental();
        item.setName(name);
        item.setPrice(price);
        item.setVolume(volume);
        item.setDate(date);
        rentalDao.insertRentalData(item);
    }

    // 插入成交榜数据
    private void handleInsertTransaction(HttpServletRequest request) throws Exception {
        String name = request.getParameter("name");
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        int volume = Integer.parseInt(request.getParameter("volume"));
        Date date = parseDate(request.getParameter("date"));

        if (transactionDao.existsByName(name)) {
            throw new Exception("饰品已存在");
        }

        transaction item = new transaction();
        item.setName(name);
        item.setPrice(price);
        item.setVolume(volume);
        item.setDate(date);
        transactionDao.insertTransactionData(item);
    }

    // 日期解析工具
    private Date parseDate(String dateStr) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return new Date(sdf.parse(dateStr).getTime());
    }
}


